<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px"
    >
      <el-form-item label="应用信息" prop="app_id">
        <el-select
          v-model="queryParams.app_id"
          placeholder="请选择应用信息"
        >
          <el-option
            v-for="item in appOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入标题"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary" icon="el-icon-search" size="mini"
          v-hasPermi="['tenant:wallet:query']" @click="handleQuery"
        >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tenant:wallet:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="钱包编号" align="center" prop="id"/>
      <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true"/>
      <el-table-column label="USDT" align="center" prop="amount" :show-overflow-tooltip="true"/>
      <el-table-column label="ETH" align="center" prop="eth_amount" :show-overflow-tooltip="true"/>
      <el-table-column label="TRX" align="center" prop="trx_amount" :show-overflow-tooltip="true"/>
      <el-table-column label="BNB" align="center" prop="bnb_amount" :show-overflow-tooltip="true"/>
      <el-table-column label="最小金额" align="center" prop="limit_min_amount" :show-overflow-tooltip="true"/>
      <el-table-column label="最大金额" align="center" prop="limit_max_amount" :show-overflow-tooltip="true"/>
      <el-table-column label="网络" align="center" prop="network">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wallet_network" :value="scope.row.network"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tenant_wallet_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" key="create_time" prop="create_time"
                       width="160"
      />
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tenant:wallet:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="refreshBalance(scope.row)"
            v-hasPermi="['tenant:wallet:query']"
          >查询余额
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleTransfer(scope.row)"
            v-hasPermi="['tenant:wallet:transfer']"
            v-if="scope.row.type == 2 || scope.row.type == 4"
          >转账
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 添加或修改平台租户钱包配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用信息" prop="app_id">
          <el-select
            v-model="form.app_id"
            placeholder="请选择应用信息"
            :disabled="form.id != undefined"
          >
            <el-option
              v-for="item in appOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address" v-if="form.type === 1 || form.type === 3">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="地址私钥" prop="private_key" v-if="form.type === 3 && form.id === undefined">
          <el-input v-model="form.private_key" placeholder="请输入地址私钥"></el-input>
        </el-form-item>
        <el-form-item label="最小金额" prop="limit_min_amount">
          <el-input type="number" step="0.01" v-model="form.limit_min_amount" placeholder="请输入最小金额"></el-input>
        </el-form-item>
        <el-form-item label="最大金额" prop="limit_max_amount">
          <el-input type="number" step="0.01" v-model="form.limit_max_amount" placeholder="请输入最大金额"></el-input>
        </el-form-item>
        <el-form-item label="网络" prop="network">
          <el-radio-group v-model="form.network">
            <el-radio
              v-for="dict in dict.type.wallet_network"
              :key="dict.value"
              :label="parseInt(dict.value)"
              :disabled="form.id != undefined"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.tenant_wallet_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
              :disabled="form.id != undefined"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示排序" prop="order_num">
          <el-input-number v-model="form.order_num" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="验证码" prop="google_code">
          <el-input v-model="form.google_code" placeholder="请输入验证码" maxlength="30"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="transferOpen" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="transferForm" :model="transferForm" :rules="rules" label-width="80px">
        <el-form-item label="地址" prop="address">
          <el-input v-model="transferForm.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input type="number" step="0.01" v-model="transferForm.amount" placeholder="请输入转账金额"></el-input>
        </el-form-item>
        <el-form-item label="币种" prop="ccy">
          <el-select
            v-model="transferForm.ccy"
            placeholder="请输入币种"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.wallet_ccy"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="验证码" prop="google_code">
          <el-input v-model="transferForm.google_code" placeholder="请输入验证码" maxlength="30"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTransferForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addTenantWallet,
  getTenantWallet,
  listTenantWallet,
  refreshBalance,
  transfer,
  updateTenantWallet
} from '@/api/tenant/tenantWallet'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {getGroupApp} from "@/api/metadata/app";
import form from "element-ui/packages/form";

export default {
  name: 'TenantWallet',
  dicts: ['sys_normal_disable', 'wallet_network', 'tenant_wallet_type', 'wallet_ccy'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台租户钱包表格数据
      dataList: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      transferOpen: false,
      // 应用信息
      appOptions: [],
      admin: false,
      // 表单参数
      form: {},
      transferForm: {},
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        app_id: undefined,
        address: undefined,
        status: 1
      },
      // 表单校验
      rules: {
        app_id: [
          {required: true, message: '应用信息不能为空', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '地址不能为空', trigger: 'blur'}
        ],
        network: [
          {required: true, message: '网络不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '状态不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '类型不能为空', trigger: 'blur'}
        ],
        amount: [
          {required: true, message: '金额不能为空', trigger: 'blur'}
        ],
        limit_min_amount: [
          {required: true, message: '最小金额不能为空', trigger: 'blur'}
        ],
        limit_max_amount: [
          {required: true, message: '最大金额不能为空', trigger: 'blur'}
        ],
        google_code: [
          {required: true, message: '验证码不能为空', trigger: 'blur'}
        ],
        private_key: [
          {required: true, message: '地址私钥不能为空', trigger: 'blur'}
        ],
        order_num: [
          {required: true, message: '显示排序不能为空', trigger: 'blur'}
        ],
        ccy: [
          {required: true, message: '币种不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.getGroupApp()
  },
  methods: {
    getGroupApp() {
      getGroupApp().then(response => {
        this.queryParams.type = 1
        this.appOptions = response.data
        this.queryParams.app_id = response.data[0].id
        this.getList()
      })
    },
    /** 查询平台租户钱包列表 */
    getList() {
      this.loading = true
      listTenantWallet(this.queryParams).then(response => {
          this.dataList = response.data
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.transferOpen = false
      this.reset()
      this.transferReset()
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        app_id: undefined,
        address: undefined,
        limit_min_amount: 0.00,
        limit_max_amount: 100000.00,
        network: 1,
        type: 1,
        status: 1,
        order_num: 0,
        google_code: undefined,
        private_key: undefined,
      }
      this.resetForm('form')
    },
    // 转账表单重置
    transferReset() {
      this.transferForm = {
        id: undefined,
        address: undefined,
        amount: undefined,
        ccy: undefined,
        google_code: undefined
      }
      this.resetForm('transferForm')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 转账操作 */
    handleTransfer(row) {
      this.transferReset()
      const id = row.id || this.ids
      this.transferForm.id = id
      this.transferOpen = true
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTenantWallet(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      })
    },
    /** 查询余额 */
    refreshBalance(row) {
      const id = row.id || this.ids
      refreshBalance(id).then(response => {
        this.$modal.msgSuccess('查询成功')
        this.getList()
      })
    },
    /** 提交按钮 */
    submitTransferForm: function () {
      this.$refs['transferForm'].validate(valid => {
        if (valid) {
          let step = async () => {
            transfer(this.transferForm).then(response => {
              this.$modal.msgSuccess('转账成功')
              this.transferOpen = false
              this.getList()
            })
          }
          step()
        }
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let step = async () => {
            if (this.form.id != undefined) {
              updateTenantWallet(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addTenantWallet(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
          step()
        }
      })
    }
  }
}
</script>
